#include "dialog_select.h"
#include "ui_dialog_select.h"

Dialog_select::Dialog_select(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog_select)
{
    ui->setupUi(this);

    connect(ui->close, SIGNAL(clicked()), this, SLOT(reject()));
}

void Dialog_select::set_selector(const Code::Selector& s)
{
    selector = s;
    emit ((ui->attack->isChecked()) ? ui->attack : ui->defence)->clicked();
    ui->draw->setCheckState((s.draw) ? Qt::Checked : Qt::Unchecked);
    ui->win->setCheckState((s.win) ? Qt::Checked : Qt::Unchecked);
    ui->disputed->setCheckState((s.disputed) ? Qt::Checked : Qt::Unchecked);
    ui->diff_from->setCurrentIndex(s.min_diff);
    ui->diff_to->setCurrentIndex(s.max_diff);
}

Dialog_select::~Dialog_select()
{
    delete ui;
}

void Dialog_select::on_attack_clicked()
{
    read_pieces(selector.attack);
    for (int i{}; i!=ui->addp->count(); ++i) {
        auto index = chess_black.indexOf(ui->addp->itemText(i));
        if (index != -1)
            ui->addp->setItemText(i, chess_red[index]);
    }
    ui->accurate->setChecked(selector.attack.is_accurate());
    emit ui->accurate->clicked(selector.attack.is_accurate());
    // ui->addp->setCurrentIndex(0);
}

void Dialog_select::on_defence_clicked()
{
    read_pieces(selector.defence);
    for (int i{}; i!=ui->addp->count(); ++i) {
        auto index = chess_red.indexOf(ui->addp->itemText(i));
        if (index != -1)
            ui->addp->setItemText(i, chess_black[index]);
    }
    ui->accurate->setChecked(selector.defence.is_accurate());
    emit ui->accurate->clicked(selector.defence.is_accurate());
    // ui->addp->setCurrentIndex(0);
}

void Dialog_select::on_add_clicked()
try
{
    Code::Pieces& pieces {(ui->attack->isChecked()) ? selector.attack : selector.defence};
    pieces.add_piece(ui->addp->currentText().front());
    read_pieces(pieces);
}
catch (Code::Full_error) {
    ui->statusbar->setText("不能再加了");
}

void Dialog_select::on_del_clicked()
try
{
    if (ui->pieces->currentRow() < 0)
        ui->statusbar->setText(tr("未选中任何项目"));
    else {
        QChar piece {ui->pieces->currentItem()->text().front()};
        Code::Pieces& pieces {(ui->attack->isChecked()) ? selector.attack : selector.defence};
        pieces.del_piece(piece);
        read_pieces(pieces);
    }
}
catch (Code::Del_king_error) {
    ui->statusbar->setText(tr("这个不能删"));
}

void Dialog_select::on_accept_clicked()
{
    selector.win = (ui->win->checkState()==Qt::Checked);
    selector.draw = (ui->draw->checkState()==Qt::Checked);
    selector.disputed = (ui->disputed->checkState()==Qt::Checked);
    selector.min_diff = ui->diff_from->currentIndex();
    selector.max_diff = ui->diff_to->currentIndex();
    accept();
}

void Dialog_select::read_pieces(const Code::Pieces& pieces)
{
    ui->pieces->clear();
    for (auto p : pieces.const_pieces())
        ui->pieces->addItem(tr("%1 %2").arg(p.first).arg(p.second));
}

void Dialog_select::on_diff_from_currentIndexChanged(int index)
{
    if (index > ui->diff_to->currentIndex()) {
        ui->diff_from->setCurrentIndex(ui->diff_to->currentIndex());
        ui->diff_to->setCurrentIndex(index);
    }
}

void Dialog_select::on_accurate_clicked(bool checked)
{
    ui->accurate->setText(tr((checked) ? "精确式搜索" : "包含式搜索"));
    Code::Pieces& pieces {(ui->attack->isChecked()) ? selector.attack : selector.defence};
    pieces.set_accurate(checked);
}

void Dialog_select::on_clear_clicked()
{
    Code::Pieces& pieces {(ui->attack->isChecked()) ? selector.attack : selector.defence};
    pieces.clear();
    read_pieces(pieces);
}

void Dialog_select::on_diff_to_currentIndexChanged(int index)
{
    if (index < ui->diff_from->currentIndex()) {
        ui->diff_to->setCurrentIndex(ui->diff_from->currentIndex());
        ui->diff_from->setCurrentIndex(index);
    }
}
